<?php
namespace Home\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Paginatior\Paginator;
use Zend\Paginator\Adapter\Null as PageNull;
use Zend\Session\Container;
use Home\Model\Video;
use Home\Model\Categories;

class IndexController extends AbstractActionController
{

	public function indexAction()
	{
		$this->layout('layout/index');
		$categories = $this->getCategoriesTable()->fetchAll();
		$this->layout()->categories = $categories;
		$Videonew = $this->getVideoTable()->getlimitvideo(0,8);
		$Videoold = $this->getVideoTable()->getlimitvideo(8,8);
		return new ViewModel(array('data'=>$Videonew,'old'=>$Videoold));
	}

	public function detailAction()
	{
		$this->layout('layout/index');
		$categories = $this->getCategoriesTable()->fetchAll();
		$this->layout()->categories = $categories;
		$id = (int) $this->params()->fromRoute('id',0);
		if (isset($id)) {
			$resultVideo =$this->getVideoTable()->getvideobyid($id);

			return new ViewModel(array('data'=>$resultVideo,'cate'=>$categories));
		}
		
	}
	public function categoriesAction()
	{
		$this->layout('layout/index');
		$categories = $this->getCategoriesTable()->fetchAll();
		$this->layout()->categories = $categories;

		$id = (int) $this->params()->fromRoute('id',0);
		if (isset($id)) {
			$cate= $this->getCategoriesTable()->getcatebyid($id);
			$paginator =$this->getCategoriesTable()->getbyCate($id);

			 	$this->layout()->action = "categories";
                $this->layout()->router = "home";
              
                $this->layout()->id = $id;
                $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
                $paginator->setItemCountPerPage(20);
                return new ViewModel (array('paginator' => $paginator,'cate'=>$cate));
		}
		
	}

	public function searchAction()
	{
		$this->layout('layout/index');
		$categories = $this->getCategoriesTable()->fetchAll();
		$this->layout()->categories = $categories;
		$data="";
        if (isset($_GET['search'])) {
            $data = $_GET['search'];
        }
        $this->layout()->action = "search";
        $this->layout()->router = "home";
        $paginator =$this->getVideoTable()->getbytitle($data);
        //var_dump($paginator);die;
        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
        $paginator->setItemCountPerPage(16);
        return new ViewModel (array('paginator' => $paginator));
	}


//*****
//**********************************************************************************************//
//*****
    protected $VideoTable;
	public function getVideoTable()
     {
         if (!$this->VideoTable) {
             $sm = $this->getServiceLocator();
             $this->VideoTable = $sm->get('Home\Model\VideoTable');
         }
         return $this->VideoTable;
     }



     protected $CategoriesTable;
    public function getCategoriesTable()
     {
         if (!$this->CategoriesTable) {
             $sm = $this->getServiceLocator();
             $this->CategoriesTable = $sm->get('Home\Model\CategoriesTable');
         }
         return $this->CategoriesTable;
     }
     //*****
//**********************************************************************************************//
//*****
}